package cn.lwalan.commonUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.lang3.StringUtils;

public class StringCalculateUtils {

	public static List<String> splitAmapping(String amapping)
	{
		List<String> agts = new ArrayList<String>();
		String[] strs = amapping.replace("|", ",").split(",");
		for(String str:strs)
		{
			if(!",".equals(str)&&!"".equals(str))
			{
				agts.add(str);
			}
		}
		return agts;
	}
	
	/**
	 * 字符串数字相减
	 * @param first 减数
	 * @param second 被减数
	 * @param keepDeciaml 是否保留小数位
	 * @return
	 */
	public static  String subStr2Str(String first,String second,boolean keepDeciaml)
	{
		if(keepDeciaml)
		{
			BigDecimal result=new BigDecimal(first).subtract(new BigDecimal(second)).setScale(3, BigDecimal.ROUND_DOWN);
			return result.toString();
		}
		else{
			BigDecimal result=new BigDecimal(first).subtract(new BigDecimal(second)).setScale(0, BigDecimal.ROUND_DOWN);
			return result.toString();
		}
		
	}
	
	/**
	 * 字符串数字相加
	 * @param first
	 * @param second
	 * @param keepDeciaml 是否保留小数点
	 * @return
	 */
	public static String addStr2Str(String first,String second,boolean keepDeciaml)
	{
		if(keepDeciaml)
		{
			BigDecimal result=new BigDecimal(first).add(new BigDecimal(second)).setScale(3, BigDecimal.ROUND_DOWN);
			return result.toString();
		}
		else{
			BigDecimal result=new BigDecimal(first).add(new BigDecimal(second)).setScale(0, BigDecimal.ROUND_DOWN);
			return result.toString();
		}
	}
	
	/**
	 * 三个字符串数字的相加
	 * @param first
	 * @param second
	 * @param third
	 * @param keepDeciaml 是否保留小数点
	 * @return
	 */
	public static String addStr2Str(String first,String second,String third,boolean keepDeciaml)
	{
		String temp=addStr2Str(first,second,keepDeciaml);
		if(keepDeciaml)
		{
			BigDecimal result=new BigDecimal(temp).add(new BigDecimal(third)).setScale(3, BigDecimal.ROUND_DOWN);
			return result.toString();
		}
		else{
			BigDecimal result=new BigDecimal(temp).add(new BigDecimal(third)).setScale(0, BigDecimal.ROUND_DOWN);
			return result.toString();
		}
	}
	
	//由于amt为分，所以不保留小数点
	public static String multiplyStr2Str(String amt,String feeRate)
	{
		
		BigDecimal result =new BigDecimal(amt).multiply(new BigDecimal(feeRate)).divide(new BigDecimal(100)).setScale(0, BigDecimal.ROUND_DOWN);
		
		return result.toString();
	}
	
	/**
	 * 将单位为元的小数转化成分的整数
	 * @param value
	 * @return
	 */
	public static String doubleStr2IntStr(String value)
	{
		BigDecimal result =new BigDecimal(value).multiply(new BigDecimal(100)).setScale(0, BigDecimal.ROUND_DOWN);
		return result.toString();
	}
	
	/**
	 * 值除以100,保留XX位小数点
	 * 
	 * @param value
	 * @param decipoint 保留小数点的位数
	 * @return
	 */
	public static String divideOneHundred(String value,int decipoint){
		if(StringUtils.isEmpty(value)){
			return null;
		}
		BigDecimal result = new BigDecimal(value).divide(new BigDecimal("100")).setScale(decipoint, BigDecimal.ROUND_DOWN);
		
		return result.toString();
	}
	
	public static void main(String[] args) {
		System.out.println(doubleStr2IntStr("0.01"));
	}
}
